<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- 添加一级部门 -->
                    <el-dropdown-item @click.native="hadd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree #default="{data}" :data="list" :props="defaultProps" default-expand-all>
          <el-row type="flex" justify="space-between" align="middle" style="height:40px;width:100%">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>{{ data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hadd(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hedit(data.id)">编辑部门</el-dropdown-item>
                      <!-- 如果有子级级部门 不显示删除 -->
                      <el-dropdown-item v-if="data.children.length === 0" @click.native="hdel(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </el-tree>
      </el-card>
      <!-- 根据布尔值修改表单标题 -->
      <el-dialog
        :title="bool?'编辑部门':'添加部门'"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <!-- v-if解决表单不更新的问题 -->
        <deptDialog v-if="dialogVisible" :id="uerId" :bool="bool" :origin-list="originList" @success="success" @close="close" />
        <!-- <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click.native="dialogVisible = false">确 定</el-button>
        </span> -->
      </el-dialog>
    </div>
  </div>
</template>
<script>

import { getDepartments, delDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils'
import deptDialog from './deptDialog.vue'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      list: [],
      dialogVisible: false,
      defaultProps: {
        children: 'children',
        label: 'manager'
      },
      // 保存id
      uerId: '',
      // 开关思想 判断是编辑还是新增
      bool: false,
      originList: []

    }
  },
  created() {
    // 渲染页面
    this.hGetDepartments()
  },
  methods: {
    hadd(id) {
      this.dialogVisible = true
      this.bool = false
      this.uerId = id
    },
    hedit(id) {
      this.dialogVisible = true
      this.bool = true
      this.uerId = id
    },
    // 删除
    hDel(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        if (res.code === 10000) {
          this.$message.success(res.message)
          // 从新渲染数据
          this.hGetDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除11'
        })
      })
    },
    // 渲染页面
    async hGetDepartments() {
      try {
        const res = await getDepartments()
        if (res.code === 10000) {
          this.originList = res.data.depts.map(item => {
            const { id, name, code, pid } = item
            return { id, name, code, pid }
          })
        }
        // console.log(res)
        // 删除第一条数据
        res.data.depts.shift()
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    success() {
      // 添加完毕 渲染页面
      this.hGetDepartments()
      // 关闭弹层
      this.dialogVisible = false
    },
    // 点击表单取消按钮 关闭弹层
    close() {
      // 关闭弹层
      this.dialogVisible = false
    }
  }
}
</script>
